package com.storeroompda.ui.main;

import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.storeroompda.R;
import com.storeroompda.base.views.BaseActivity_noMVP;
import com.storeroompda.dialogs.ChooseDialog;
import com.storeroompda.ui.basesync.BaseSyncActivity;
import com.storeroompda.ui.changestore.ChangeStoreActivity;
import com.storeroompda.ui.enterstore.EnterStoreActivity;
import com.storeroompda.ui.returnstore.ReturnStoreActivity;
import com.storeroompda.ui.salesstore.SalesStoreActivity;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends BaseActivity_noMVP {
    private String tag = "MainActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.gridview)
    GridView gridview;

    private int[] imageRes = {
            R.mipmap.u60,
            R.mipmap.u59,
            R.mipmap.u61,
            R.mipmap.u58,
            R.mipmap.u62
    };

    private String[] name = {
            "生产入库",
            "销售出库",
            "调拨出库",
            "退货入库",
            "数据同步"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        tvTitle.setText("主页");

        ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < imageRes.length; i++) {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", imageRes[i]);//添加图像资源的ID
            map.put("ItemText", name[i]);//按序号做ItemText
            lstImageItem.add(map);
        }
        //生成适配器的ImageItem 与动态数组的元素相对应
        SimpleAdapter saImageItems = new SimpleAdapter(this,
                lstImageItem,//数据来源
                R.layout.item_main,//item的XML实现
                //动态数组与ImageItem对应的子项
                new String[]{"ItemImage", "ItemText"},
                //ImageItem的XML文件里面的一个ImageView,两个TextView ID
                new int[]{R.id.img_shoukuan, R.id.txt_shoukuan});
        //添加并且显示
        gridview.setAdapter(saImageItems);
        //添加消息处理
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    intentToActivityWithoutParameter(MainActivity.this, EnterStoreActivity.class);
                } else if (position == 1) {
                    intentToActivityWithoutParameter(MainActivity.this, SalesStoreActivity.class);
                } else if (position == 2) {
                    intentToActivityWithoutParameter(MainActivity.this, ChangeStoreActivity.class);
                } else if (position == 3) {
                    intentToActivityWithoutParameter(MainActivity.this, ReturnStoreActivity.class);
                } else if (position == 4) {
                    intentToActivityWithoutParameter(MainActivity.this, BaseSyncActivity.class);
                }
            }
        });
    }

    @OnClick({R.id.iv_title_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                ChooseDialog nDialog = new ChooseDialog(this, R.style.Dialog1, new ChooseDialog.PriorityListener() {
                    @Override
                    public void setActivityText() {
                        finish();
                    }
                },"退出程序");
                WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
                lp.gravity = Gravity.CENTER;
                nDialog.show();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            ChooseDialog nDialog = new ChooseDialog(this, R.style.Dialog1, new ChooseDialog.PriorityListener() {
                @Override
                public void setActivityText() {
                    finish();
                }
            },"退出程序");
            WindowManager.LayoutParams lp = nDialog.getWindow().getAttributes();
            lp.gravity = Gravity.CENTER;
            nDialog.show();
        }
        return super.onKeyDown(keyCode, event);
    }
}
